//
//  RectRadarAnimationView.swift
//  RectRadarAnimationView
//
//  Created by damon on 2019/6/25.
//  Copyright © 2019 damon. All rights reserved.
//
import UIKit

public extension UIView {
    
    /// 添加带圆角的矩形雷达动画，使用 view.addRadarAnimation(size: size)
    ///
    /// - Parameters:
    ///   - beginSize: 初始size
    ///   - fillColor: 填充色，默认 .white
    ///   - increment: 扩展范围
    ///   - beginAlpha: 初始透明度
    func addRadarAnimation(beginSize size: CGSize, fillColor: UIColor = .white, expand increment: CGFloat = 30, beginAlpha: CGFloat = 0.5) -> RectRadarAnimationView {
        let radarView = RectRadarAnimationView(beginFrame: CGRect(origin: .zero, size: size), expand: increment, fillColor: fillColor, beginAlpha: beginAlpha)
        self.superview?.insertSubview(radarView, belowSubview: self)
//        radarView.snp.makeConstraints {
//            $0.center.equalTo(self)
//            $0.size.equalTo(radarView.frame.size)
//        }
        
        // todofix:
        DispatchQueue.main.async {
            radarView.center = self.center
            radarView.size = self.size
        }
        
        radarView.addAnimation()
        return radarView
    }
    
    /// 添加带圆角的矩形雷达动画，使用 view.addRadarAnimation()
    ///
    /// - Parameters:
    ///   - inset: 内边距
    ///   - fillColor: 填充颜色，默认 .white
    ///   - increment: 扩展范围
    ///   - beginAlpha: 初始透明度
    func addRadarAnimation(inset: UIEdgeInsets = .zero, fillColor: UIColor = .white, expand increment: CGFloat = 30, beginAlpha: CGFloat = 0.5) -> RectRadarAnimationView {
        let radarFrame = frame.inset(by: inset)
        return addRadarAnimation(beginSize: radarFrame.size, fillColor: fillColor, expand: increment, beginAlpha: beginAlpha)
    }
}

